package cn.dsp.admin.service.impl;


import cn.dsp.admin.entity.admin.Role;
import cn.dsp.admin.entity.lot.LList;
import cn.dsp.admin.entity.lot.Lot;
import cn.dsp.admin.mapper.lot.LotMapper;
import cn.dsp.admin.mapper.lot.SpaceMapper;
import cn.dsp.admin.service.ILotService;
import cn.dsp.admin.service.ex.InsertException;
import cn.dsp.admin.service.ex.UpdateException;
import cn.dsp.admin.service.ex.UserNotFoundException;
import cn.dsp.admin.service.ex.UsernameConflictException;
import cn.dsp.admin.utils.DistanceUtils;
import cn.dsp.admin.utils.IteratorUtils;
import cn.dsp.admin.vo.ListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LotServiceImpl implements ILotService {

    @Autowired
    private LotMapper lotMapper;

    @Autowired
    private SpaceMapper spaceMapper;

    @Override
    public Lot getByLid(Integer lotId) {

        // 根据参数id查询停车场数据
        Lot result = lotMapper.findById(lotId);

        // 判断查询结果是否为null
        if (result == null) {
            // 是：用户数据不存在，抛出UserNotFoundException
            throw new UserNotFoundException("数据不存在");
        }


        Lot lot = new Lot();
        lot.setLotId(lotId);
        lot.setName(result.getName());
        lot.setLocation(result.getLocation());
        lot.setPrice(result.getPrice());


        return lot;
    }

    @Override
    public void updateDistance(double longitude, double latitude) {
        Integer count=lotMapper.findBigId();
        try {
            for (int i = 1; i <= count; i++) {
                double Longitude = lotMapper.findLongitude(i);
                double Latitude = lotMapper.findLatitude(i);
                DistanceUtils distanceUtils=new DistanceUtils();
                Integer distance = (int)distanceUtils.getDistance(longitude,latitude,Longitude,Latitude);
                System.out.println(distance);
                lotMapper.updateDistance(i, distance);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<ListVO> getByKey(String word,Integer storeId) {
        String keyWord="%"+word+"%";
        List<Integer> lotIds=lotMapper.findIdByLike(keyWord);
        List<ListVO> lists=new ArrayList<>();

        List<Lot> lots=new ArrayList<>();
        for (int i=0;i<lotIds.size();i++) {
            Lot lot = lotMapper.findById(lotIds.get(i));
            lots.add(lot);
        }
        lots = new IteratorUtils().LotIterator(lots,storeId);

        for (int i=0;i<lots.size();i++) {
            Lot lot=lots.get(i);
            ListVO list = new ListVO();
            list.setLotId(lotIds.get(i));
            list.setName(lot.getName());
            list.setLocation(lot.getLocation());
            list.setPrice(lot.getPrice());
            list.setEmpty(spaceMapper.findById(lot.getLotId()));
            lists.add(list);
        }
        return lists;
    }

    @Override
    public List<Lot> getAllLot(Integer storeId) {

        return findLotAll();
    }

    @Override
    public List<ListVO> getAllList(List<Lot> lots) {
        List<ListVO> data= new ArrayList<>();
        for (int i=0;i<lots.size();i++){
            ListVO list=new ListVO();
            list.setLotId(lots.get(i).getLotId());
            list.setName(lots.get(i).getName());
            list.setLocation(lots.get(i).getLocation());
            list.setPrice(lots.get(i).getPrice());
            list.setEmpty(spaceMapper.findById(lots.get(i).getLotId()));
            data.add(list);
        }
        return data;
    }

    @Override
    public ListVO getLotByLotId(Integer lotId) {
        ListVO data=new ListVO();
        Lot lot=lotMapper.findById(lotId);
        data.setLotId(lotId);
        data.setName(lot.getName());
        data.setLocation(lot.getLocation());
        data.setPrice(lot.getPrice());
        data.setAllSpace(spaceMapper.findAllById(lotId));
        data.setEmpty(spaceMapper.findById(lotId));
        return data;
    }

    @Override
    public void deleteById(Integer lotId) {
        lotMapper.deleteById(lotId);
    }

    @Override
    public void addLot(Lot lot) {
        String name = lot.getName();
        if (name.equals("")){
            throw new InsertException("角色名称不能为空！！");
        }
        Lot result = lotMapper.findByName(name);
        if (result != null) {
            throw new UsernameConflictException("角色名:"+name+",已存在！");
        }
        lotMapper.save(lot);
    }

    @Override
    public void updateLot(Lot lot) {
        Integer rows=lotMapper.updateINfo(lot);
        if (rows!=1){
            throw new UpdateException("更新数据时发生位置错误！");
        }

    }

    @Override
    public List<String> getIsLocks(Integer lotId) {
        List<Integer> result=lotMapper.findIsLocks(lotId);
        if(result==null){
           throw new UserNotFoundException("数据不存在");
        }
        List<String> data= new ArrayList<>();
        for (int i=0;i<result.size();i++){
            if(result.get(i)==0){
                data.add("#4cae4c");
            }else{
                data.add("#2aabd2");
            }
        }
        return data;
    }

    @Override
    public void updateIsLock(Integer lotId, Integer spaceId) {
        Integer isLock=lotMapper.findIsLock(lotId,spaceId);
        if (isLock==1){
            lotMapper.updateIsLock(lotId,spaceId,0);
        }else if (isLock==0){
            lotMapper.updateIsLock(lotId,spaceId,1);
        }else{
            throw new UserNotFoundException("数据不存在");
        }
    }

    private List<Lot> findLotAll(){

        return lotMapper.findLotAll();
    }
}
